package com.gree.controller;

import java.io.PrintWriter;
import com.gree.utils.Files;
import com.gree.utils.MessageID;
import com.gree.utils.StringTool;
import com.gree.entity.CmnC006User;
import javax.servlet.http.HttpSession;
import com.gree.entity.MstM001Company;
import com.gree.entity.CmnC008User2UserGroup;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.gree.service.IMstM001CompanyService;
import org.springframework.stereotype.Controller;
import com.gree.service.ICmnC008User2UserGroupService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MstM001CompanyController {

	@Autowired
	private IMstM001CompanyService companyService;

	@Autowired
	private ICmnC008User2UserGroupService userGroupService;

	@RequestMapping(value = "/comp/check", method = RequestMethod.GET)
	public void checks(String companyId, HttpSession session,HttpServletResponse response, HttpServletRequest request)	throws Exception {
		CmnC006User user = (CmnC006User) session.getAttribute("user");
		String result = null;
		if (user != null) {
			CmnC008User2UserGroup groups = userGroupService	.checkTransferPower(user.getUserId());
			if (groups == null) {
				result = StringTool.getJSONObject("error",MessageID.CHECK_POWER);
			} else {
				MstM001Company cmps = companyService.checkCompCodeExists(companyId);

				if (cmps == null) {
					result = StringTool.getJSONObject("error",MessageID.COMP_NOT_EXIST.replace("#COMP_CODE#",companyId));
				} else {
					MstM001Company obj = new MstM001Company();
					obj.setCompanyId(companyId);
					obj.setUserId(user.getUserId());
					MstM001Company cmp = companyService.checkCompCodePower(obj);
					if (cmp == null) {
						result = StringTool.getJSONObject("error",MessageID.COMPANY_CODE_CHECK.replace("#COMP_CODE#", companyId));
					} else {
						result = StringTool.getJSONObject("success",cmp.getDescription());
					}
				}
			}
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.print(result);// 返回登录信息
			out.flush();
			out.close();
		} else {
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.println("<html>");
			out.println("<script>");
			out.println("window.open ('" + request.getContextPath()+ "/Views/index.jsp','_top')");
			out.println("</script>");
			out.println("</html>");
		}
	}

	@RequestMapping(value = "/comp/uploads", produces = "text/html;charset=utf-8", method = RequestMethod.POST)
	public void readerFile(String companyId, String loginId,@RequestParam(value = "fileField") MultipartFile fileField,HttpServletResponse response) throws Exception {
		String result = Files.getFileUpload(companyId, loginId, loginId,fileField);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(result);// 返回登录信息
		out.flush();
		out.close();
		//ReadFileAlone.getReadFile();
	}

}
